package com.yint.tree;

import java.util.HashMap;
import java.util.Map;

/**
 * 白板
 * Created by 殷田 on 2018/5/7.
 */
public class WhiteBoard {

    private static WhiteBoard instance;

    private Map<String, Object> dataMap = new HashMap<String, Object>();

    private WhiteBoard() {

    }

    public boolean isExist(String key) {
        return dataMap.containsKey(key);
    }

    public static WhiteBoard get() {
        if (instance == null) {
            synchronized (WhiteBoard.class) {
                if (instance == null) {
                    instance = new WhiteBoard();
                }
            }
        }
        return instance;
    }

    public void add(String key, Object data) {
        if (dataMap.containsKey(key)) {
            dataMap.remove(key);
        }
        dataMap.put(key, data);
    }

    public final void addString(String key, String data) {
        add(key, data);
    }

    public final void addInt(String key, int data) {
        add(key, data);
    }

    public final void addFloat(String key, float data) {
        add(key, data);
    }

    public final void addLong(String key, long data) {
        add(key, data);
    }

    public final void addBoolean(String key, boolean data) {
        add(key, data);
    }

    public final void addByte(String key, byte data) {
        add(key, data);
    }

    public Object get(String key) {
        if (dataMap.containsKey(key)) {
            return dataMap.get(key);
        }
        return null;
    }

    public String getString(String key) {
        return (String) dataMap.get(key);
    }

    public boolean getBoolean(String key) {
        return (boolean) dataMap.get(key);
    }

    public int getInt(String key) {
        return (int) dataMap.get(key);
    }

    public long getLong(String key) {
        return (long) dataMap.get(key);
    }

    public float getFloat(String key) {
        return (float) dataMap.get(key);
    }

    public double getDouble(String key) {
        return (double) dataMap.get(key);
    }

    public byte getByte(String key) {
        return (byte) dataMap.get(key);
    }

    public void remove(String key) {
        if (dataMap.containsKey(key)) {
            dataMap.remove(key);
        }
    }

    public void clear() {
        dataMap.clear();
    }
}
